package cn.iocoder.yudao.module.water.service.prohis;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.water.controller.admin.prohis.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.prohis.ProHisDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.water.convert.prohis.ProHisConvert;
import cn.iocoder.yudao.module.water.dal.mysql.prohis.ProHisMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.water.enums.ErrorCodeConstants.*;

/**
 * 生产历史 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class ProHisServiceImpl implements ProHisService {

    @Resource
    private ProHisMapper proHisMapper;

    @Override
    public Long createProHis(ProHisCreateReqVO createReqVO) {
        // 插入
        ProHisDO proHis = ProHisConvert.INSTANCE.convert(createReqVO);
        proHisMapper.insert(proHis);
        // 返回
        return proHis.getId();
    }

    @Override
    public void updateProHis(ProHisUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProHisExists(updateReqVO.getId());
        // 更新
        ProHisDO updateObj = ProHisConvert.INSTANCE.convert(updateReqVO);
        proHisMapper.updateById(updateObj);
    }

    @Override
    public void deleteProHis(Long id) {
        // 校验存在
        this.validateProHisExists(id);
        // 删除
        proHisMapper.deleteById(id);
    }

    private void validateProHisExists(Long id) {
        if (proHisMapper.selectById(id) == null) {
            throw exception(PRO_HIS_NOT_EXISTS);
        }
    }

    @Override
    public ProHisDO getProHis(Long id) {
        return proHisMapper.selectById(id);
    }

    @Override
    public List<ProHisDO> getProHisList(Collection<Long> ids) {
        return proHisMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProHisDO> getProHisPage(ProHisPageReqVO pageReqVO) {
        return proHisMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProHisDO> getProHisList(ProHisExportReqVO exportReqVO) {
        return proHisMapper.selectList(exportReqVO);
    }

}
